/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.astrob.nhcmap.R;

import zuo.biao.library.base.BaseModel;

public class NaviTrackSetInfo extends BaseModel {

    private static long serialVersionUID = 1L;

    //航迹设置
    private static String NAVITRACKINFO = "navitrackinfo";

    private static String NAVITRACKSET_MODE = "navitrackset_mode";//记录模式
    private static String NAVITRACKSET_INTERVAL_T = "navitrackset_interval_t";//时间记录间隔
    private static String NAVITRACKSET_INTERVAL_D = "navitrackset_interval_d";//距离记录间隔
    private static String NAVITRACKSET_AUTO = "navitrackset_auto";//自动保存轨迹
    private static String NAVITRACKSET_QIYAGD = "navitrackset_qiyagd";//使用气压传感器海拔高度
    private static String NAVITRACKSET_PHFZ = "navitrackset_phfz_str";//偏航阀值


    //自动：时速小于10km时，按5s间隔，时速10KM-30KM时，按3s间隔，时速大于30KM时，按1s间隔
    public static String[] SAVEMODE_NAMES = {"时间", "距离", "自动"};
    public static String[] INTERVALSTR_TIMES = {"1秒", "5秒", "10秒", "15秒", "30秒", "60秒"};
    public static String[] INTERVALSTR_DISTANCES = {"5米", "10米", "15米", "20米", "50米"};
    public static int[] INTERVAL_TIMES = {1, 5, 10, 15, 30, 60};
    public static int[] INTERVAL_DISTANCES = {5, 10, 15, 20, 50};
    public static String[] AUTOMODE_NAMES = {"当记录满时", "每天", "每周"};

    private int savemode = 0;//记录模式 0-时间 1-距离 2-自动
    private int intervalmode_t = 0;//时间记录间隔 //
    private int intervalmode_d = 1;//距离记录间隔 //
    private int automode = 0;//自动保存轨迹 0-当记录满时  1-每天  2-每周
    private boolean useqiyagd = false;//使用气压传感器海拔高度
    private double phfz = 30;//偏航阀值 米
    private boolean saveendclear = true;//航迹保存后清除当前轨迹

    private static NaviTrackSetInfo instance = new NaviTrackSetInfo();

    public static NaviTrackSetInfo getInstance() {
        return instance;
    }

    /**
     * 默认构造方法，JSON等解析时必须要有
     */
    public NaviTrackSetInfo() {
        //default
    }

    public void save(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                NAVITRACKINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(NAVITRACKSET_MODE, this.savemode);
        editor.putInt(NAVITRACKSET_INTERVAL_T, this.intervalmode_t);
        editor.putInt(NAVITRACKSET_INTERVAL_D, this.intervalmode_d);
        editor.putInt(NAVITRACKSET_AUTO, this.automode);
        editor.putBoolean(NAVITRACKSET_QIYAGD, this.useqiyagd);
        editor.putString(NAVITRACKSET_PHFZ, this.phfz+"");
        editor.putBoolean("saveendclear", this.saveendclear);
        editor.commit();
    }

    public void load(Context context) {
        if (context == null) {
            return;
        }

        SAVEMODE_NAMES = context.getResources().getStringArray(R.array.navitracksetinfo_savemode_names);
        INTERVALSTR_TIMES = context.getResources().getStringArray(R.array.navitracksetinfo_intervakstr_times);
        INTERVALSTR_DISTANCES = context.getResources().getStringArray(R.array.navitracksetinfo_intervakstr_distances);
        AUTOMODE_NAMES = context.getResources().getStringArray(R.array.navitracksetinfo_automode_names);

        SharedPreferences sp = context.getSharedPreferences(
                NAVITRACKINFO, Context.MODE_PRIVATE);
        this.savemode = sp.getInt(NAVITRACKSET_MODE, this.savemode);
        this.intervalmode_t = sp.getInt(NAVITRACKSET_INTERVAL_T, this.intervalmode_t);
        this.intervalmode_d = sp.getInt(NAVITRACKSET_INTERVAL_D, this.intervalmode_d);
        this.automode = sp.getInt(NAVITRACKSET_AUTO, this.automode);
        this.useqiyagd = sp.getBoolean(NAVITRACKSET_QIYAGD, this.useqiyagd);
        this.saveendclear = sp.getBoolean("saveendclear", this.saveendclear);
        this.phfz = Double.valueOf(sp.getString(NAVITRACKSET_PHFZ, this.phfz + ""));
    }

    public void reset() {
        savemode = 0;//记录模式
        intervalmode_t = 0;//记录间隔
        intervalmode_d = 1;//记录间隔
        automode = 0;//自动保存轨迹
        useqiyagd = false;//使用气压传感器海拔高度
        phfz = 30;//偏航阀值 米
        saveendclear = true;
    }

    public String getSavemodeStr() {
//         0-时间 1-距离 2-自动
        if (savemode < 0 || savemode >= SAVEMODE_NAMES.length) {
            savemode = 0;
        }
        return SAVEMODE_NAMES[savemode];
    }

    public String getAutomodeStr() {
        if (automode < 0 || automode >= AUTOMODE_NAMES.length) {
            automode = 0;
        }
        return AUTOMODE_NAMES[automode];
    }

    public String getIntervalStr() {
        if (savemode == 0) {
            if (intervalmode_t < 0 || intervalmode_t >= INTERVALSTR_TIMES.length) {
                intervalmode_t = 1;
            }
            return INTERVALSTR_TIMES[intervalmode_t];
        } else if (savemode == 1) {
            if (intervalmode_d < 0 || intervalmode_d >= INTERVALSTR_DISTANCES.length) {
                intervalmode_d = 1;
            }
            return INTERVALSTR_DISTANCES[intervalmode_d];
        }

        return "5 秒";
    }

    public int getInterval_T() {
        if (intervalmode_t < 0 || intervalmode_t >= INTERVAL_TIMES.length) {
            intervalmode_t = 1;
        }
        return INTERVAL_TIMES[intervalmode_t];
    }

    public int getInterval_D() {
        if (intervalmode_d < 0 || intervalmode_d >= INTERVAL_DISTANCES.length) {
            intervalmode_d = 1;
        }
        return INTERVAL_DISTANCES[intervalmode_d];
    }

    public int getIntervalmode() {
        if (savemode == 0) {
            return intervalmode_t;
        }
        return intervalmode_d;
    }

    public void setIntervalmode(int intervalmode) {
        if (savemode == 0) {
            this.intervalmode_t = intervalmode;
        } else {
            this.intervalmode_d = intervalmode;
        }
    }

    public int getSavemode() {
        return savemode;
    }

    public void setSavemode(int savemode) {
        this.savemode = savemode;
    }

    public int getAutomode() {
        return automode;
    }

    public void setAutomode(int automode) {
        this.automode = automode;
    }

    public boolean isUseqiyagd() {
        return useqiyagd;
    }

    public void setUseqiyagd(boolean useqiyagd) {
        this.useqiyagd = useqiyagd;
    }

    public double getPhfz() {
        return phfz;
    }

    public void setPhfz(double phfz) {
        this.phfz = phfz;
    }

    public int getIntervalmode_t() {
        return intervalmode_t;
    }

    public void setIntervalmode_t(int intervalmode_t) {
        this.intervalmode_t = intervalmode_t;
    }

    public int getIntervalmode_d() {
        return intervalmode_d;
    }

    public void setIntervalmode_d(int intervalmode_d) {
        this.intervalmode_d = intervalmode_d;
    }

    public boolean isSaveendclear() {
        return saveendclear;
    }

    public void setSaveendclear(boolean saveendclear) {
        this.saveendclear = saveendclear;
    }

    @Override
    protected boolean isCorrect() {//根据自己的需求决定，也可以直接 return true
        return id > 0;// && StringUtil.isNotEmpty(phone, true);
    }

}
